<template>
	<view class="activity">
		<block-bar :title="'奖励'"></block-bar>
		<view class="power">
			<view class="power-c">
				<view class="power-list power-left">
					<view class="power-text">
						短算力(sg)
					</view>
					<view class="power-num" @click="goPage('/pages/activity/powers/powers')">
						<text>{{temporary_power.toFixed(1)}}</text>
						<text class="iconfont icon-active-dayu"></text>
					</view>
				</view>
				<view class="power-list power-right">
					<view class="power-text">
						长算力(lg)
					</view>
					<view class="power-num" @click="goPage('/pages/activity/powers/powers')">
						<text>{{calculationPower}}</text>
						<text class="iconfont icon-active-dayu"></text>
					</view>
				</view>
				<view class="wenti">
					<text class="iconfont icon-wenti"></text>
				</view>
			</view>
		</view>
		<view class="sign">
			<view class="lists">
				<view class="lists-c">
					<view class="list" v-for="(item,index) of signData" :key="index" :style="{background:item.isSigin?'#de2042':'#f5f6f6',
					color:item.isSigin?'#ffffff':'#2C405A'}">
						<image src="../../static/images/fudai.png" mode=""></image>
						<view class="list-jl">
							<text class="iconfont icon-jiasu"></text>
							<text>{{item.power}}</text>
						</view>
					</view>
				</view>
				<view class="btn" v-if="!status">
					<text @click="goSign()">立即签到</text>
				</view>
			</view>
		</view>
		<view class="activity-s">
			<view class="activity-lists">
				<view class="activity-list" v-for="(item,index) of taskList" :key="index">
					<view class="activity-list-left">
						<view class="list-left">
							<text class="title">{{item.title}}</text>
							<text class="label" :style="{
								background:item.label_bg,
								color:item.label_coloe
							}">{{item.label}}{{item.max_num>0?item.max_num*item.reward_power :''}}</text>
						</view>
						<text class="toast">
							{{item.brief}}{{item.max_num>0?item.complete_num+'/'+item.max_num+'次' :''}}
						</text>
					</view>
					<text class="btn btn-complete"
						v-if="item.max_num !=0 && item.complete_num == item.max_num">已完成</text>
					<text v-else class="btn" @click="goPage(item.go_url)">{{item.btn_txt}}</text>
				</view>
			</view>
			<!-- #ifdef APP-PLUS -->
			<news-flow :type="1"></news-flow>
			<!-- #endif -->
		</view>
		<uni-popup class="popup" ref="popup" type="center" >
			<view class="popup-center" :class="$isPC ? 'IsPC':''">
				<view class="popup-top">
					<view class="popup-title">
						双倍奖励提醒
					</view>
					<view class="popup-txt">
						观看视频可领取双倍奖励
					</view>
				</view>
				<view class="popup-btns">
					<view class="popup-btn" @click="ptSign()">普通签到</view>
					<view class="popup-btn sb" @click="sbSign()">双倍奖励</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import BlockBar from '@/components/BlockBar/BlockBar.vue'
	import {
		check,
		sign,
		getPowerNum
	} from '@/api/power.js'
	import {
		getTaskLists,
		completeTask
	} from '@/api/activity.js'
	import {
		mapState
	} from "vuex"
	import AD from "@/utils/ad.js"
	export default {
		name: "activity",
		components: {
			BlockBar,
		},
		data() {
			return {
				signData: [],
				status: 0, //是否签到
				permanent_power: 0, //永久算力
				temporary_power: 0, //临时算力
				taskList: [], //任务列表
			}
		},
		computed: {
			...mapState(['userInfo', 'isLogin']),
			calculationPower() {
				return (Number(this.userInfo.power) + this.permanent_power).toFixed(1)
			}
		},
		onShow() {
			this.getCheck();
			this.getTasks();
			this.powerNum();
		},
		methods: {
			async getTasks() {
				let res = await getTaskLists();
				if (res.code === 1) {
					this.taskList = res.data
				}
			},
			goPage(url, keyword) {
				if (!this.isLogin) {
					return uni.navigateTo({
						url: '/pages/entrance/login/login'
					})
				}
				if (url === '/seevideo') {
					/* #ifdef H5 */
					this.$msg("请下载App体验")
					/* #endif */
					// #ifdef APP-PLUS
					this.showRewardedVideoAd()
					// #endif
					return
				}
				if (url === '/duoliang') {
					/* #ifdef H5 */
					this.$msg("请下载App体验")
					/* #endif */
					// #ifdef APP-PLUS
					var duoliangModule = uni.requireNativePlugin("duoliang-DuoLiangTaskModule");
					var params = {
						'userId': this.userInfo.id,
						'advertType': 0,
						'title': '游戏中心'
					}
					duoliangModule.jumpTaskList(params);
					// #endif
					return
				}
				uni.navigateTo({
					url: url
				})
			},
			async powerNum() {
				let res = await getPowerNum();
				if (res.code === 1) {
					this.permanent_power = res.data.permanent_power;
					this.temporary_power = res.data.temporary_power;
				}
			},
			async getCheck() {
				let res = await check();
				if (res.code === 1) {
					let data = res.data.lists;
					let num = res.data.signs.sign_num
					data.forEach((item, index) => {
						if (index < num) {
							item.isSigin = true
						} else {
							item.isSigin = false
						}
					})
					this.signData = data
					this.status = res.data.signs.status
				}
			},
			goSign() {
				if (!this.isLogin) {
					return uni.navigateTo({
						url: '/pages/entrance/login/login'
					})
				}
				if (this.status) {
					this.$msg("今天已签到")
					return
				}
				this.$refs.popup.open();
			},
			//普通签到
			async ptSign(){
				let res = await sign();
				if (res.code === 1) {
					this.getCheck()
					this.powerNum()
				}else{
					this.$msg(res.msg)
				}
				this.$refs.popup.close()
			},
			async sbSign(){
				/* #ifdef H5 */
				this.$msg("请下载App完成签到")
				/* #endif */
				// #ifdef APP-PLUS
				this.showRewardedVideoAd("sign");
				// #endif
			},
			// 激励视频
			showRewardedVideoAd(type) {
				// 调用后会显示 loading 界面
				AD.show({
					adpid: 1341390077,
					adType: "RewardedVideo",
					//singleton: false // 设置此参数后，每次调用 show 方法将重新创建广告实例，预载将失效，如果广告回调每次要透传用户信息需要设置 false
				}, async (res) => {
					// 用户点击了【关闭广告】按钮
					if (res && res.isEnded) {
						// 正常播放结束
						if(type === "sign"){
							let res = await sign({type:2});
							if (res.code === 1) {
								this.getCheck()
							}else{
								this.$msg(res.msg)
							}
							this.$refs.popup.close()
						}else{
							let data = await completeTask({keyword:"see_video"})
							this.getTasks();
						}
						this.powerNum()
					} else {
						// 播放中途退出
						this.$msg("播放中途退出") 
					}
				}, (err) => {
					console.log(err)
				})
			},
		}
	}
</script>

<style scoped lang="less" src="./index.less">
</style>
